﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;
using CDStore3Tiers;
using DevComponents.DotNetBar;

namespace CuaHangDiaNhac
{
	public partial class frmPhanQuyen : DevComponents.DotNetBar.Metro.MetroForm
	{
		log4net.ILog log = log4net.LogManager.GetLogger("Phân quyền");
		StoreCDDataContext db;
		public frmPhanQuyen()
		{
			InitializeComponent();
			db = new StoreCDDataContext();
			db.Connection.ConnectionString = getConfig.getConnection();
		}


		XLPhanQuyen xl = new XLPhanQuyen();
		private string _id;
		string ten;
		XuLyForm XLF = new XuLyForm();
		List<vwQuyenHan> listcn = new List<vwQuyenHan>();

		List<ChucNang> listDS = new List<ChucNang>();



		public frmPhanQuyen(string id, string tennv)
		{
			InitializeComponent();
			db = new StoreCDDataContext();
			db.Connection.ConnectionString = getConfig.getConnection();
			LoadDGVR(id);
			LoadDGVL();
			this.Text += " " + id.ToString();
			_id = id;
			listDS = xl.DSChucNang();
			txtTenTaiKhoan.Text = tennv;
			ten = tennv;
			txtTaiKhoan.Text = id;
		}


		public void LoadDSR(string id)
		{
			var dsl = db.QuyenHan_SelectByID(id);
			BindingSource qh = new BindingSource();
			qh.DataSource = dsl;
			bindingNavigator1.BindingSource = qh;
			dgvCTPQ.DataSource = qh;
			listcn = xl.selectChucNang(id);
		}

		DataGridViewTextBoxColumn colID;
		DataGridViewTextBoxColumn colMaCN;
		DataGridViewTextBoxColumn colChucNang;
		public void LoadDGVR(string id)
		{
			dgvCTPQ.DataSource = null;

			colID = new DataGridViewTextBoxColumn();
			colID.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			colID.DataPropertyName = "ID";
			colID.HeaderText = "Tài khoản";
			colID.Name = "cnameID";
			colID.Visible = false;
			dgvCTPQ.Columns.Add(colID);

			colMaCN = new DataGridViewTextBoxColumn();
			colMaCN.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			colMaCN.DataPropertyName = "TKChucNang";
			colMaCN.HeaderText = "Mã chức năng";
			colMaCN.Name = "cnameMaCN";
			dgvCTPQ.Columns.Add(colMaCN);

			colChucNang = new DataGridViewTextBoxColumn();
			colChucNang.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			colChucNang.DataPropertyName = "ChucNangPM";
			colChucNang.HeaderText = "Chức năng";
			colChucNang.Name = "cnameChucNang";
			dgvCTPQ.Columns.Add(colChucNang);

			LoadDSR(id);
		}


		public void LoadDSCN()
		{
			var dsdn = db.ChucNang_SelectAll();
			BindingSource ds = new BindingSource();
			ds.DataSource = dsdn;
			bindingNavigator2.BindingSource = ds;
			dgvDSCN.DataSource = ds;
			dgvDSCN.Refresh();
		}

		DataGridViewTextBoxColumn colMaCNL;
		DataGridViewTextBoxColumn colChucNangL;
		DataGridViewTextBoxColumn colQuyenHanMD;
		public void LoadDGVL()
		{
			dgvDSCN.DataSource = null;

			colMaCNL = new DataGridViewTextBoxColumn();
			colMaCNL.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			colMaCNL.DataPropertyName = "MaChucNang";
			colMaCNL.HeaderText = "Mã chức năng";
			colMaCNL.Name = "cnameMaCNL";
			dgvDSCN.Columns.Add(colMaCNL);

			colChucNangL = new DataGridViewTextBoxColumn();
			colChucNangL.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			colChucNangL.DataPropertyName = "ChucNangPM";
			colChucNangL.HeaderText = "Chức năng";
			colChucNangL.Name = "cnameChucNangL";
			dgvDSCN.Columns.Add(colChucNangL);

			colQuyenHanMD = new DataGridViewTextBoxColumn();
			colQuyenHanMD.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			colQuyenHanMD.DataPropertyName = "QuyenHanMacDinh";
			colQuyenHanMD.HeaderText = "Quyền hạn mặc định";
			colQuyenHanMD.Name = "cnameQHMD";
			dgvDSCN.Columns.Add(colQuyenHanMD);

			LoadDSCN();
		}

		private void bttAddex_Click(object sender, EventArgs e)
		{
			navigationPane1.Expanded = true;
			log.Info(frmLogin.id + ": đã chọn chức năng thêm chức năng vào tài khoản");
		}
		private void tsRefesh_Click(object sender, EventArgs e)
		{
			LoadDGVL();
			LoadDGVR(_id);
			log.Info(frmLogin.id + ": đã làm mới danh sách phân quyền");
		}
		private void tsDelete_Click(object sender, EventArgs e)
		{
			//db = new StoreCDDataContext();
			DatHang dh = new DatHang();

			if (dgvCTPQ.SelectedRows.Count > 0)
			{
				DialogResult accept = DevComponents.DotNetBar.MessageBoxEx.Show("Bạn có chắc chắn muốn xóa ?", "thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
				if (accept == DialogResult.Yes)
				{
					for (int i = 0; i < dgvCTPQ.SelectedRows.Count; i++)
					{
						try
						{
							int tkcn = Convert.ToInt32(dgvCTPQ.SelectedRows[i].Cells["cnameMaCN"].Value);
							string cn = dgvCTPQ.SelectedRows[i].Cells["cnameChucNangL"].Value.ToString();

							var xoadh = db.TaiKhoanChucNang_Delete(_id, tkcn);
							log.Info(frmLogin.id + ": đã xóa chức năng" + cn + "ra khỏi tài khoản" + _id);
							db.SubmitChanges();

						}
						catch
						{
							DevComponents.DotNetBar.MessageBoxEx.Show("Ops! Đã xảy ra lỗi!...");
							log.Info(frmLogin.id + ": xóa chức năng ra khỏi tài khoản thất bại");
						}
					}
				}
				LoadDGVR(_id);
			}
		}

		private void tsTimkiem_Leave(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(tsTimkiem.Text))
			{
				tsTimkiem.Text = "Nhập giá trị cần tìm kiếm !";
				XLF.loadDGV(bindingNavigator1, dgvCTPQ, listcn);
			}
		}
		private void tsTimkiem_MouseUp(object sender, MouseEventArgs e)
		{
			tsTimkiem.Clear();
		}
		private void tsTimkiem_TextChanged(object sender, EventArgs e)
		{
			xl = new XLPhanQuyen();
			XLF.loadDGV(bindingNavigator1, dgvCTPQ, xl.searchByID(listcn, tsTimkiem.Text));
		}
		private void LockContextMenuR(bool p)
		{
			bttAddex.Enabled = p;

			bttXoaex.Enabled = p;
			bttRefeshex.Enabled = p;
		}
		private void LockContextMenuL(bool p)
		{
			bttThemDNVaoCTHD.Enabled = p;
		}
		private void dgvCTDH_MouseDown(object sender, MouseEventArgs e)
		{
			DataGridView.HitTestInfo hit;
			if (e.Button == MouseButtons.Right)
			{
				hit = dgvCTPQ.HitTest(e.X, e.Y);
				if (hit.Type == DataGridViewHitTestType.Cell)
				{
					if (!((DataGridViewRow)(dgvCTPQ.Rows[hit.RowIndex])).Selected)
					{
						dgvCTPQ.ClearSelection();
						((DataGridViewRow)dgvCTPQ.Rows[hit.RowIndex]).Selected = true;
					}
				}
			}
			if (dgvCTPQ.SelectedRows.Count > 1)
				LockContextMenuR(false);
			else
				LockContextMenuR(true);
		}

		private void bttThemDNVaoCTHD_Click(object sender, EventArgs e)
		{

			for (int i = 0; i < dgvDSCN.SelectedRows.Count; i++)
			{
				int macn = ConvertType.ToInt(dgvDSCN.SelectedRows[i].Cells["cnameMaCNL"].Value.ToString());
				try
				{
					dgvCTPQ.DataSource = db.TaiKhoanChucNang_Insert(_id, macn);
				}
				catch (Exception)
				{ }
			}
			LoadDGVR(_id);
		}

		private void bttAddex_Click_1(object sender, EventArgs e)
		{
			navigationPane1.Expanded = true;
		}

		private void toolStripTextBox3_TextChanged(object sender, EventArgs e)
		{
			xl = new XLPhanQuyen();
			XLF.loadDGV(bindingNavigator1, dgvDSCN, xl.searchByDSCN(listDS, toolStripTextBox3.Text));
		}

		private void toolStripTextBox3_MouseUp(object sender, MouseEventArgs e)
		{
			toolStripTextBox3.Clear();
		}

		private void toolStripTextBox3_Leave(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(tsTimkiem.Text))
			{
				toolStripTextBox3.Text = "Nhập giá trị cần tìm kiếm !";
				XLF.loadDGV(bindingNavigator2, dgvDSCN, listDS);
			}
		}

		private void bttRefeshex_Click(object sender, EventArgs e)
		{
			LoadDGVL();
			LoadDGVR(_id);
		}

		private void bttXoaex_Click(object sender, EventArgs e)
		{
			tsDelete.PerformClick();
		}

		private void dgvCTPQ_MouseDown(object sender, MouseEventArgs e)
		{
			DataGridView.HitTestInfo hit;
			if (e.Button == MouseButtons.Right)
			{
				hit = dgvCTPQ.HitTest(e.X, e.Y);
				if (hit.Type == DataGridViewHitTestType.Cell)
				{
					if (!((DataGridViewRow)(dgvCTPQ.Rows[hit.RowIndex])).Selected)
					{
						dgvCTPQ.ClearSelection();
						((DataGridViewRow)dgvCTPQ.Rows[hit.RowIndex]).Selected = true;
					}
				}
			}
			if (dgvCTPQ.SelectedRows.Count > 1)
				LockContextMenuR(false);
			else
				LockContextMenuR(true);
		}

		private void dgvDSCN_MouseDown(object sender, MouseEventArgs e)
		{
			DataGridView.HitTestInfo hit;
			if (e.Button == MouseButtons.Right)
			{
				hit = dgvDSCN.HitTest(e.X, e.Y);
				if (hit.Type == DataGridViewHitTestType.Cell)
				{
					if (!((DataGridViewRow)(dgvDSCN.Rows[hit.RowIndex])).Selected)
					{
						dgvDSCN.ClearSelection();
						((DataGridViewRow)dgvDSCN.Rows[hit.RowIndex]).Selected = true;
					}
				}
			}
			if (dgvDSCN.SelectedRows.Count > 1)
				LockContextMenuL(false);
			else
				LockContextMenuL(true);
		}

		private void frmPhanQuyen_Load(object sender, EventArgs e)
		{
			LoadDGVL();
            panel1.BackColor = Color.FromArgb(0,192,0);
			log.Info(frmLogin.id + ": đã load lên form phân quyền");
		}

	}

}
